package com.migu.rbac.controller;

import com.migu.rbac.model.SysRole;
import com.migu.rbac.service.SysRoleMenuService;
import com.migu.rbac.service.SysRoleService;
import com.migu.rbac.vo.GlobalConst;
import com.migu.rbac.vo.Response;
import com.migu.rbac.vo.page.RolePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @Description TODO : 角色
 * @Author: zj
 * @Date: 11:24 2019/4/3
 * @Version 1.0
 **/
@RestController
@RequestMapping(path = "/api/v1/role")
public class RoleController {

    @Autowired
    SysRoleMenuService roleMenuService;

    @Autowired
    SysRoleService roleService;


    //条件 分页查询所有角色
    @PostMapping("/role-page")
    private Response menuPage(@RequestBody RolePage rolePage) {
        Response result = null;
        try {
            result = Response.success(roleService.selectRoleByExample(rolePage));
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //添加角色
    @PostMapping(value = "/addRole", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response addRole(@RequestBody SysRole sysRole) {
        Response result = null;
        try {
            if (roleService.saveRole(sysRole)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "角色已经存在或添加失败！");
            };
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //删除角色
    @DeleteMapping(value = "/deleteRole/{roleid}")
    public Response deleteRole(@PathVariable int roleid) {
        Response result = null;
        try {
            if (roleService.deleteRole(roleid)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "删除失败！");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //修改角色
    @PutMapping(value = "/updateRole")
    public Response updateRole(@RequestBody SysRole sysRole) {
        Response result = null;
        try {
            if (roleService.updateRole(sysRole)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, " 修改角色失败！");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //查询所有角色
    @GetMapping(value = "/getRole")
    public Response getRole() {
        Response result = null;
        try {
            result = Response.success(roleService.findAllRole());
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //查询角色下所有的菜单id
    @GetMapping(value = "/getMenuIds/{roleid}")
    public Response getMenuIds(@PathVariable int roleid) {
        Response result = null;
        try {
            result = Response.success(roleMenuService.selectMenuByRoleId(roleid));
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //分配角色权限
    @PostMapping(value = "/saveRoleMenu", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response saveRoleMenu(@RequestParam("roleid") int roleid,
                                 @RequestBody int[] menus) {
        Response result = null;
        try {
            if (roleMenuService.updateRoleMenu(roleid, menus)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, " 分配角色权限失败！");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }
}
